package com.system.vote.common.utils;


import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

public class DateUtil {

    private DateUtil() {
    }

    public static String currentTimeStr() {
        Date now = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddhhmmssSSSS");
        return simpleDateFormat.format(now);
    }

    public static Temporal dateToTemporal(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    public static Date temporalToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date stringToDate(String dateTimeString) {
        DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        ZoneId zoneId = ZoneId.of("Asia/Shanghai", ZoneId.SHORT_IDS);
        ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateTimeString, inputFormatter);
        zonedDateTime = zonedDateTime.withZoneSameInstant(zoneId);
        LocalDateTime dateTime = zonedDateTime.toLocalDateTime();
        return Date.from(dateTime.toInstant((ZoneOffset) zoneId));
    }

    /**
     * @param type(day,week,month,year)
     * @param count(天,周,月,年）
     * @return
     */
    public static List<Map<String, Object>> getRecentDateRange(String type, int count) {
        List<Map<String, Object>> recentDateRange = new ArrayList<>();
        LocalDate today = LocalDate.now();
        LocalDate dateAgo = null;

        if (Objects.equals(type, "day")) {
            dateAgo = today.minusDays(count);
        }
        if (Objects.equals(type, "week")) {
            dateAgo = today.minusWeeks(count);
        }
        if (Objects.equals(type, "month")) {
            dateAgo = today.minusMonths(count);
        }
        if (Objects.equals(type, "year")) {
            dateAgo = today.minusYears(count);
        }
        LocalDate date = dateAgo;
        while (date != null && date.isBefore(today)) {
            ZonedDateTime startOfDay = null;
            ZonedDateTime endOfDay = null;
            Map<String, Object> result = new HashMap<>();
            // 在这里添加你的代码逻辑
            if (Objects.equals(type, "day")) {
                date = date.plusDays(1);
                // 获取该日期的开始时间
                startOfDay = date.atStartOfDay(ZoneId.systemDefault());
                // 获取该日期的结束时间
                endOfDay = date.atTime(23, 59, 59).atZone(ZoneId.systemDefault());
            }
            if (Objects.equals(type, "week")) {
                date = date.plusWeeks(1);
                DayOfWeek dayOfWeek = date.getDayOfWeek();
                LocalDate firstDayOfWeek = today.with(TemporalAdjusters.previousOrSame(dayOfWeek));
                LocalDate lastDayOfWeek = today.with(TemporalAdjusters.nextOrSame(dayOfWeek));
                LocalDateTime firstDayOfRange = firstDayOfWeek.atStartOfDay();
                LocalDateTime lastDayOfRange = lastDayOfWeek.atTime(23, 59, 59);
                startOfDay = ZonedDateTime.ofInstant(firstDayOfRange.toInstant(ZoneOffset.ofTotalSeconds(0)), ZoneId.systemDefault());
                endOfDay = ZonedDateTime.ofInstant(lastDayOfRange.toInstant(ZoneOffset.ofTotalSeconds(0)), ZoneId.systemDefault());

            }
            if (Objects.equals(type, "month")) {
                date = date.plusMonths(1);
                // 获取当前月份
                YearMonth currentYearMonth = YearMonth.from(date);
                // 获取当前月份的第一天
                LocalDate firstDayOfMonth = currentYearMonth.atDay(1);
                // 获取当前月份的最后一天
                LocalDate lastDayOfMonth = currentYearMonth.atEndOfMonth();

                // 计算时间范围从第一天 00:00:00 到最后一天 23:59:59
                LocalDateTime firstDayOfRange = firstDayOfMonth.atStartOfDay();
                LocalDateTime lastDayOfRange = lastDayOfMonth.atTime(23, 59, 59);
                startOfDay = ZonedDateTime.ofInstant(firstDayOfRange.toInstant(ZoneOffset.ofTotalSeconds(0)), ZoneId.systemDefault());
                endOfDay = ZonedDateTime.ofInstant(lastDayOfRange.toInstant(ZoneOffset.ofTotalSeconds(0)), ZoneId.systemDefault());

            }
            if (Objects.equals(type, "year")) {
                date = date.minusYears(1);
                LocalDate firstDayOfYear = today.with(TemporalAdjusters.firstDayOfYear()); // 获取当年第一天
                LocalDate lastDayOfYear = today.with(TemporalAdjusters.lastDayOfYear()); // 获取当年最后一天
                LocalDateTime firstDateTimeOfYear = firstDayOfYear.atTime(0, 0, 0); // 获取当年第一天 00:00:00
                LocalDateTime lastDateTimeOfYear = lastDayOfYear.atTime(23, 59, 59); // 获取当年最后一天 23:59:59
                startOfDay = ZonedDateTime.ofInstant(firstDateTimeOfYear.toInstant(ZoneOffset.ofTotalSeconds(0)), ZoneId.systemDefault());
                endOfDay = ZonedDateTime.ofInstant(lastDateTimeOfYear.toInstant(ZoneOffset.ofTotalSeconds(0)), ZoneId.systemDefault());
            }
            if (startOfDay != null) {
                result.put("startDate", Date.from(startOfDay.toInstant()));
            }
            if (endOfDay != null) {
                result.put("endDate", Date.from(endOfDay.toInstant()));
            }
            recentDateRange.add(result);
        }
        return recentDateRange;
    }
}
